package com.sgb.kjwl.viewmodel.pick;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.pick.PickSearchResult;
import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.model.entity.pick.PickSearchType;
import com.sgb.kjwl.model.entity.pick.TenderHistoryEntity;
import com.sgb.kjwl.repository.PickRepertory;
import com.sgb.kjwl.view.ui.activity.pick.PickSearchActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PickSearchModel extends BaseViewModel {

    private PickSearchActivity activity;
    private PickRepertory repertory;
    private Map map;

    public PickSearchModel(PickSearchActivity pickSearchActivity, PickRepertory repertory) {
        this.activity = pickSearchActivity;
        this.repertory = repertory;
    }

    /**
     * 获取搜索结果数目
     */
    public void findMenuList(String keyword){
        if (map == null)
            map = new HashMap();
        map.clear();
        map.put("areacode", repertory.getAreaCode());
        map.put("keyword", keyword);
        DialogHelper.showProgressDialog(activity, null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getfindMenuListData(map).enqueue(new ICallBack<BaseEntity<PickSearchType>>() {
            @Override
            public void onSuccess(BaseEntity<PickSearchType> data) {
                if (data == null ) return;
                if (data.getData() == null) return;
                if (data.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    activity.showfindMenu(data.getData(),keyword);
                }
            }
        });
    }

    public void addSearchRecord(String searchName){
        if (map == null)
            map = new HashMap();
        map.clear();
        map.put("searchName", searchName);
        DialogHelper.showProgressDialog(activity, null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().addSearchRecord(map).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {

            }
        });
    }

    public void findListBySearchName(String searchName){
        if (map == null)
            map = new HashMap();
        map.clear();
        map.put("searchName", searchName);
        ArrayList<String> searchList = new ArrayList<>();
        DialogHelper.showProgressDialog(activity, null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().findListBySearchName(map).enqueue(new ICallBack<BaseEntity<List<TenderHistoryEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<TenderHistoryEntity>> data) {
                if (data == null) return;
                if (data.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    if (data.getData() != null) {
                        for (TenderHistoryEntity tenderHistoryEntity : data.getData()) {
                            searchList.add(tenderHistoryEntity.getSearchName());
                        }
                    }
                    activity.showData(searchList);
                }
            }
        });
    }

    public void findListSearch(String searchName){
        if (map == null)
            map = new HashMap();
        map.clear();
        map.put("keyword", searchName);
//        DialogHelper.showProgressDialog(activity, null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().findListSearch(map).enqueue(new ICallBack<BaseEntity<List<PickSearchResult>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickSearchResult>> data) {
                if (data.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    activity.showSearchData(data.getData());
                }
            }
        });
    }

    public void deleteUserAllRecord(){
        DialogHelper.showProgressDialog(activity, null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().deleteUserAllRecord().enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {

            }
        });
    }
}
